using UnityEngine;
using System.Collections;



[System.Serializable]
public class SidescrollPlacer
{
	#region Variables
		#region Gameplay area
			public	SidescrollGameplayArea	m_sgaGameplayArea;
		#endregion
		
		#region Position
			public	Vector2					m_v2PosRelative			= Vector2.zero;
			public	eSidescrollLimitMode	m_eLimitationMode; 
		#endregion
		
		#region Orientation
			public	bool					m_bOrienting			= true;
			public	bool					m_bShipOrienting		= true;
			public	bool					m_bOrientationBlending	= true;
			public	float					m_fOrientBlendTimeTotal;
			public	float					m_fOrientBlendTimeCur;
			public	Quaternion				m_qLastOrientation;
		#endregion
	#endregion
	
	
	#region Propertys
	//---------------------------------------------------------------------------------------------------------
	//---------------------------------------------------------------------------------------------------------
	public bool IsOrienting
	{
		get{
			return m_bOrienting;
		}
		set{
			if(!m_bOrienting && value)
			{
				m_fOrientBlendTimeCur = 0;
			}
			m_bOrienting	=	value;
		}
	}
	
	public eSidescrollLimitMode LimitationMode
	{
		get{
			return m_eLimitationMode;
		}
		set{
			m_eLimitationMode	=	value;
		}
	}
	#endregion
	
	//---------------------------------------------------------------------------------------------------------
	//---------------------------------------------------------------------------------------------------------
	public void ApplyLimitation(ref Vector2 _v2Position, ref bool _bLimitedX, ref bool _bLimitedY)
	{
		switch(m_eLimitationMode)
		{
			case eSidescrollLimitMode.HardLimited:
				if(_v2Position.x 	>	m_sgaGameplayArea.m_v3Size.x)
				{
					_v2Position.x 	= 	m_sgaGameplayArea.m_v3Size.x;
					_bLimitedX 		= 	true;
				}
				else if(_v2Position.x < 	- m_sgaGameplayArea.m_v3Size.x)
				{
					_v2Position.x 	= 	- m_sgaGameplayArea.m_v3Size.x;
					_bLimitedX 		= 	true;
				}
				if(_v2Position.y 	> 	m_sgaGameplayArea.m_v3Size.y)
				{
					_v2Position.y 	= 	m_sgaGameplayArea.m_v3Size.y;
					_bLimitedY 		= 	true;
				}
				else if(_v2Position.y < 	- m_sgaGameplayArea.m_v3Size.y)
				{
					_v2Position.y 	= 	- m_sgaGameplayArea.m_v3Size.y;
					_bLimitedY 		= 	true;
				}
			break;
			
		}
	}
	
	//---------------------------------------------------------------------------------------------------------
	//---------------------------------------------------------------------------------------------------------
	public void ApplyLimitation(ref Vector3 _v3Position, ref bool _bLimitedX, ref bool _bLimitedY, ref bool _bLimitedZ)
	{
		switch(m_eLimitationMode)
		{
			case eSidescrollLimitMode.HardLimited:
				if(_v3Position.x 	>	m_sgaGameplayArea.m_v3Size.x)
				{
					_v3Position.x 	= 	m_sgaGameplayArea.m_v3Size.x;
					_bLimitedX 		= 	true;
				}
				else if(_v3Position.x < 	- m_sgaGameplayArea.m_v3Size.x)
				{
					_v3Position.x 	= 	- m_sgaGameplayArea.m_v3Size.x;
					_bLimitedX 		= 	true;
				}
				if(_v3Position.y 	> 	m_sgaGameplayArea.m_v3Size.y)
				{
					_v3Position.y 	= 	m_sgaGameplayArea.m_v3Size.y;
					_bLimitedY 		= 	true;
				}
				else if(_v3Position.y < 	- m_sgaGameplayArea.m_v3Size.y)
				{
					_v3Position.y 	= 	- m_sgaGameplayArea.m_v3Size.y;
					_bLimitedY 		= 	true;
				}
				if(_v3Position.z 	> 	m_sgaGameplayArea.m_v3Size.z)
				{
					_v3Position.z 	= 	m_sgaGameplayArea.m_v3Size.z;
					_bLimitedZ 		= 	true;
				}
				else if(_v3Position.z <- m_sgaGameplayArea.m_v3Size.z)
				{
					_v3Position.z 	= 	- m_sgaGameplayArea.m_v3Size.z;
					_bLimitedZ 		= 	true;
				}
			break;
			
		}
	}
	 
	//---------------------------------------------------------------------------------------------------------
	//---------------------------------------------------------------------------------------------------------
	public void UpdateAndMove(Transform _otMe, ref Vector2 _v2Displacement)
	{
		m_v2PosRelative	+=	_v2Displacement;
		PlaceAndOrient(_otMe, ref m_v2PosRelative);
	}
	
	//---------------------------------------------------------------------------------------------------------
	//---------------------------------------------------------------------------------------------------------
	public void UpdateAndPlace(Transform _otMe, ref Vector2 _v2LocalPos)
	{
		m_v2PosRelative = _v2LocalPos;
		PlaceAndOrient(_otMe, ref _v2LocalPos);
	}
	public void UpdateAndPlace(Transform _otMe, ref Vector3 _v3LocalPos)
	{
		PlaceAndOrient(_otMe, ref _v3LocalPos);
	}
	
	//---------------------------------------------------------------------------------------------------------
	//---------------------------------------------------------------------------------------------------------
	private void PlaceAndOrient(Transform _tTarget, ref Vector2 _v2LocalPos)//, ref bool _bLimitedX, ref bool _bLimitedY)
	{
		bool bDummie				=	false;
		ApplyLimitation(ref _v2LocalPos, ref bDummie, ref bDummie);
		_tTarget.position			=	m_sgaGameplayArea.GetWorldPositionOnItFromRelativePos(ref _v2LocalPos); 
		if(m_bOrienting)
		{
			if(m_sgaGameplayArea.bOrientingShips)
			{
				m_qLastOrientation	=	m_sgaGameplayArea.m_tOrienter.rotation;
			}
			else
			{
				UpdateOrientaitonBlend();
			}
			_tTarget.rotation		=	m_qLastOrientation;
		}
	}	
	private void PlaceAndOrient(Transform _tTarget, ref Vector3 _v3LocalPos)//, ref bool _bLimitedX, ref bool _bLimitedY, ref bool _bLimitedZ)
	{
		bool bDummie				=	false;
		ApplyLimitation(ref _v3LocalPos, ref bDummie, ref bDummie, ref bDummie);
		_tTarget.position			=	m_sgaGameplayArea.GetWorldPositionOnItFromRelativePos(ref _v3LocalPos); 
		if(m_sgaGameplayArea.bOrientingShips)
		{
			if(m_bShipOrienting)
			{
				m_qLastOrientation	=	m_sgaGameplayArea.m_tOrienter.rotation;
			}
			UpdateOrientaitonBlend();
			_tTarget.rotation		=	m_qLastOrientation;
		}
	}
	
	//---------------------------------------------------------------------------------------------------------
	//---------------------------------------------------------------------------------------------------------
	public void UpdateOrientaitonBlend()
	{
		if(m_bOrientationBlending)
		{
			m_fOrientBlendTimeCur 		+= Time.deltaTime;
			if(m_fOrientBlendTimeCur >= m_fOrientBlendTimeTotal)
			{
				m_bOrientationBlending	= false;
				m_qLastOrientation		=	m_sgaGameplayArea.transform.rotation;
			}
			else
			{
				m_qLastOrientation 		= Quaternion.Slerp(m_qLastOrientation, m_sgaGameplayArea.transform.rotation, m_fOrientBlendTimeCur/m_fOrientBlendTimeTotal);
			}
		}
		else
		{
			m_qLastOrientation			=	m_sgaGameplayArea.transform.rotation;
		}
	}
}

